/**
 * 
 */
package com.jie.eclipse.pan.bind;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;

import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.swtitem.ListGroup;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class BaseDataBind {
	protected List<String> paramList = null;
	protected Map<String, Object> ctrMap = null;
	
	public BaseDataBind(){
		this.ctrMap = new HashMap<String, Object>();
		this.paramList = new ArrayList<String>();
	}
	
	/**
	 * @param name
	 * @param obj
	 * 控件绑定
	 */
	public void bind(String name,Object obj){
		this.paramList.add(name.toLowerCase());
		this.ctrMap.put(name.toLowerCase(), obj);
	}
	
	public void unbind(String name){
		this.paramList.remove(name);
		this.ctrMap.remove(name);
	}
	
	public Object getCtr(String name){
		return this.ctrMap.get(name.toLowerCase());
	}
	
	public Button getButtonCtr(String name){
		Object obj = this.ctrMap.get(name.toLowerCase());
		if(obj instanceof Button)
			return (Button)obj;
		else 
			return null;
	}
	
	public Combo getComboCtr(String name){
		Object obj = this.ctrMap.get(name.toLowerCase());
		if(obj instanceof Combo)
			return (Combo)obj;
		else 
			return null;
	}
	
	public Text getTextCtr(String name){
		Object obj = this.ctrMap.get(name.toLowerCase());
		if(obj instanceof Text)
			return (Text)obj;
		else 
			return null;
	}
	
	
	/**
	 * @return
	 */
	public BaseDataEntity getRecord(){
		BaseDataEntity record = new BaseDataEntity();
		for (int i = 0; i < paramList.size(); i++) {
			String name = paramList.get(i);
			Object obj = this.ctrMap.get(name);
			record.SetParamValue(name, this.getValueByCtr(obj));
		}
		return record;
	}
	
	public void cloneTo(BaseDataBind bind,boolean bclear){
		if(bclear)	bind.clear();
		for (int i = 0; i < paramList.size(); i++) {
			String name = paramList.get(i);	
			Object obj = this.ctrMap.get(name);
			bind.bind(name, obj);
		}
	}
	
	public void clear(){
		this.paramList.clear();
		this.ctrMap.clear();
	}
	
	public void reset(){
		for (int i = 0; i < paramList.size(); i++) {
			String name = paramList.get(i);
			
			Object obj = this.ctrMap.get(name);
			Object value = "";
			if(obj == null)
				continue;
			
			if(obj instanceof Button){
				Button btn = (Button)obj;
				if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
					if(value.equals(btn.getData())){
						btn.setSelection(true);
					}
				}
			}else if(obj instanceof Combo){
				Combo combo = (Combo)obj;
				for (int j = 0; j < combo.getItemCount(); j++) {
					if(value.equals(combo.getData(String.valueOf(j)))){
						combo.select(j);
						break;
					}
				}
			}else if(obj instanceof Text){
				Text text = (Text)obj;
				text.setText(String.valueOf(value));
			}else if(obj instanceof ListGroup){
				ListGroup listGroup = (ListGroup)obj;
				listGroup.reset();
			}else if(obj instanceof BaseDataBind){
				BaseDataBind bind = (BaseDataBind)obj;
				bind.reset();
			}
		}
	}
	
	public void setRecord(BaseDataEntity record){
		for (int i = 0; i < paramList.size(); i++) {
			String name = paramList.get(i);
			if(!record.ContainesParam(name)) continue;
			Object value = record.gpov(name);
			setValue(name,value);
		}
	}
	
	public void setValue(String name,Object value){
		Object obj = this.ctrMap.get(name.toLowerCase());
		if(obj == null)
			return ;
		if(obj instanceof Button){
			Button btn = (Button)obj;
			if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
				if(value.equals(btn.getData()) || String.valueOf(value).equals(String.valueOf(btn.getData()))){
					btn.setSelection(true);
					btn.notifyListeners(SWT.Selection, new Event());//手动触发选择事件
				}
			}
		}else if(obj instanceof Combo){
			Combo combo = (Combo)obj;
			for (int j = 0; j < combo.getItemCount(); j++) {
				if(value.equals(combo.getData(String.valueOf(j)))){
					combo.select(j);
					break;
				}
			}
		}else if(obj instanceof Text){
			Text text = (Text)obj;
			text.setText(String.valueOf(value));
		}else if(obj instanceof Label){
			Label lab = (Label)obj;
			lab.setText(String.valueOf(value));
		}else if(obj instanceof DateTime){
			DateTime date = (DateTime)obj;
			Date _val = null;
			if(value instanceof Date){
				_val = (Date)value;
			}else if(value instanceof String){
				try{
					SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
					_val=df.parse(String.valueOf(value));
				}catch(Exception e){}
			}
			if(_val != null){
				Calendar cal = Calendar.getInstance();
				cal.setTime(_val);
				date.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
			}
			
		}else if(obj instanceof ListGroup){
			ListGroup listGroup = (ListGroup)obj;
			listGroup.setValues(value);
		}else if(obj instanceof BaseDataBind){
			BaseDataBind bind = (BaseDataBind)obj;
			if(value instanceof BaseDataEntity)
				bind.setRecord((BaseDataEntity)value);
		}
	}
	
	public String getValue(String name){
		return StringHelper.getValue(getValueByCtr(this.getCtr(name.toLowerCase())));
	}
	
	/**
	 * @param obj
	 * @return
	 * 取控件 的值
	 */
	public Object getValueByCtr(Object obj){
		if(obj == null)
			return null;
		if(obj instanceof Button){
			Button btn = (Button)obj;
			if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
				if(btn.getSelection())
					return btn.getData();
				else
					return "";
			}
		}else if(obj instanceof Combo){
			Combo combo = (Combo)obj;
			return combo.getData(String.valueOf(combo.getSelectionIndex()));
		}else if(obj instanceof Text){
			Text text = (Text)obj;
			return text.getText();
		}else if(obj instanceof DateTime){
			DateTime date = (DateTime)obj;
			DecimalFormat df = new DecimalFormat("00");
			return date.getYear()+"/"+df.format(date.getMonth()+1)+"/"+df.format(date.getDay());
		}else if(obj instanceof Label){
			return ((Label)obj).getText();
		}else if(obj instanceof ListGroup){
			ListGroup listGroup = (ListGroup)obj;
			return listGroup.getValues();
		}else if(obj instanceof BaseDataBind){
			BaseDataBind bind = (BaseDataBind)obj;
			return bind.getRecord();
		}
		return null;
	}
}
